package com.credithc.signature.manager.controller;

import com.credithc.baseserv.core.codes.CoreCodes;
import com.credithc.baseserv.core.message.Message;
import com.credithc.baseserv.core.message.MessageFactory;
import com.credithc.signature.common.utils.UUIDUtil;
import com.credithc.signature.manager.utils.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhanghanqiu181212@credithc.com
 * @Date 2020/4/16 10:45
 */
@RestController
@RequestMapping("/manager/scs/v2/until")
public class UntilController {


    @Autowired
    MessageFactory messageFactory;

    /**
     * 上传文件
     *
     * @param file
     */
    @PostMapping("/jpg/upload")
    public Message upload(@RequestParam("file") MultipartFile file) {
        String path = uploadInfo(file);

        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, path);
    }

    private String uploadInfo(MultipartFile file){
        String img = null;
        try {
            File path = new File("/tmp/files/img/");
            if (!path.exists()) {
                path.mkdirs();
            }
            File file1 = new File(path + File.separator + UUIDUtil.getUUID() + ".jpg");
            FileUploadUtil.inputStreamToFile(file.getInputStream(), file1);
            img = FileUploadUtil.upload(file1, "img", file.getOriginalFilename(), UUIDUtil.getUUID());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return FileUploadUtil.DOWNLOAD_PATH+img;
    }


}
